package cn.littlehx.simple_score_manage.service.impl;

import cn.littlehx.simple_score_manage.common.MyUtils;
import cn.littlehx.simple_score_manage.dao.IScoreDao;
import cn.littlehx.simple_score_manage.dao.IStudentDao;
import cn.littlehx.simple_score_manage.domain.Score;
import cn.littlehx.simple_score_manage.domain.Student;
import cn.littlehx.simple_score_manage.domain.Teacher;
import cn.littlehx.simple_score_manage.service.IStudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<IStudentDao, Student> implements IStudentService {
    @Autowired
    private IScoreDao scoreDao;
    @Autowired
    private IStudentDao studentDao;

    @Override
    public List<Map<String, String>> selectScoreList(Integer id) {
        return studentDao.selectCourseById(id);
    }

    @Override
    public boolean save(Student entity) {
        if (entity.getPassword() != null) {
            //增加MD5加密
            entity.setPassword(MyUtils.encode(entity.getPassword()));
        } else {
            //增加默认密码000000并进行加密
            //增加MD5加密
            entity.setPassword(MyUtils.encode("000000"));
        }
        return super.save(entity);
    }

    @Override
    public boolean updateById(Student entity) {
        if (entity.getPassword() != null&&!entity.getPassword().equals("")) {
//            log.info("进入加密,加密前密码为:"+entity.getPassword());
            //增加MD5加密
            entity.setPassword(MyUtils.encode(entity.getPassword()));
//            log.info("加密后密码为:"+entity.getPassword());
        }else {
            entity.setPassword(null);
        }
        return super.updateById(entity);
    }

    @Override
    public Student selectByNoAndPassword(String no, String password) {
//        对密码进行加密
        password = MyUtils.encode(password);
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getNo,no);
        wrapper.eq(Student::getPassword,password);
        List<Student> list = this.list(wrapper);
        return list.get(0);
    }


}
